﻿using Goway.StackingUC.Forms;
using Goway.StackingUC.Modules;
using Sunny.UI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Goway.StackingUC
{
    public partial class FrmMainBak : UIForm
    {
        /// <summary>
        /// 仓库管理
        /// </summary>
        private WarehouseManager warehouseMng;

        /// <summary>
        /// 是否点击了“关闭”按钮
        /// </summary>
        private bool isCloseButtonClick;

        /// <summary>
        /// 选择的库区编号
        /// </summary>
        private int selectedAreaId;


        public FrmMain()
        {
            InitializeComponent();

            isCloseButtonClick = false;
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            isCloseButtonClick = true;
            FrmTips frm = new FrmTips("确定停止堆垛机上位程序，退出系统吗？");
            if(frm.ShowDialog() == DialogResult.OK)
            {
                this.Close();
            }
        }

        private void FrmMain_FormClosing(object sender, FormClosingEventArgs e)
        {
            if(isCloseButtonClick)
            {
                return;
            }

            FrmTips frm = new FrmTips("确定停止堆垛机上位程序，退出系统吗？");
            if (frm.ShowDialog() != DialogResult.OK)
            {
                e.Cancel = true;
            }

            isCloseButtonClick = false;
        }

        private void timerMessage_Tick(object sender, EventArgs e)
        {
            MarqueeMessage.ShowMarqueeMessage_Tick(this);
        }

        private void FrmMain_Load(object sender, EventArgs e)
        {
            this.ShowMarqueeMessage("页面加载中...");

            warehouseMng = new WarehouseManager();
            // 事件绑定
            warehouseMng.Add2ModuleManager += WarehouseMng_Add2ModuleManager;
            warehouseMng.ShowWndMessage += this.ShowMessage;

            if (!warehouseMng.Initialization())
            {
                this.ShowMarqueeMessage("仓库管理模块初始化失败！");
                return;
            }

            // 加载库区信息
            List<WarehouseArea> areaList = warehouseMng.GetAreaList();
            if(areaList == null || !areaList.Any())
            {
                this.ShowMessage("没有合法的库区信息");
                this.ShowMarqueeMessage("没有合法的库区信息");
            }
            else
            {
                this.cmbArea.DisplayMember = "Name";
                this.cmbArea.ValueMember = "Id";
                this.cmbArea.DataSource = areaList;
            }

            #region 测试代码
            /*
            Dictionary<int, int> dictItems = new Dictionary<int, int>();
            Dictionary<int, int> destItems = new Dictionary<int, int>();
            dictItems.Add(3, 6);
            destItems.Add(9, 15);
            destItems.Add(3, 18);
            dictItems = DictionaryHelper.AppentRange<int, int>(dictItems, destItems, true);
            */
            #endregion
        }

        /// <summary>
        /// 追加处理线程到线程管理模块
        /// </summary>
        /// <param name="funcName"></param>
        /// <param name="arge"></param>
        /// <returns></returns>
        private bool WarehouseMng_Add2ModuleManager(string funcName, Action<object> arge)
        {
            ModuleManager moduleManager = ModuleManager.GetInstance();
            if(moduleManager != null)
            {
                moduleManager.AddRunFunction(funcName, arge);
                return true;
            }

            return false;
        }

        /// <summary>
        /// 入库测试
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnInputBarcode_Click(object sender, EventArgs e)
        {
            if(selectedAreaId <= 0)
            {
                this.ShowMessage("请选择库区！");
                this.ShowMarqueeMessage("请选择库区！");
            }

            // 向所选择库区增加物料
            if (string.IsNullOrEmpty(this.txtBarcode.Text.Trim()))
            {
                this.ShowMessage("请录入物料条码！");
                this.ShowMarqueeMessage("请录入物料条码！");
            }
             
            if(this.warehouseMng.AddMaterial2Area(this.selectedAreaId, this.txtBarcode.Text.Trim(), out string errorMsg))
            {
                this.ShowMessage($"增加物料-{this.txtBarcode.Text.Trim()}成功！");
                this.ShowMarqueeMessage($"增加物料-{this.txtBarcode.Text.Trim()}成功！");
            }
            else
            {
                this.ShowMessage(errorMsg);
                this.ShowMarqueeMessage(errorMsg);
            }
        }

        /// <summary>
        /// 键盘按下事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void txtBarcode_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                btnInputBarcode_Click(null, null);
            }
        }

        #region 已删除
        /*
        /// <summary>
        /// 显示消息
        /// </summary>
        /// <param name="msg"></param>
        public void ShowMessage(string msg)
        {
            if (!this.Created)
            {
                return;
            }

            if (this.txtMessage.InvokeRequired && !txtMessage.IsDisposed)
            {
                this.Invoke(new MethodInvoker(delegate
                {
                    this.txtMessage.Text = this.txtMessage.Text.Insert(0, DateTime.Now.ToString("t") + " : " + msg + "\n");
                }));
            }
            else if (!txtMessage.IsDisposed)
            {
                this.txtMessage.Text = this.txtMessage.Text.Insert(0, DateTime.Now.ToString("t") + " : " + msg + "\n");
            }
        }
        */
        #endregion

        private void cmbArea_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.cmbArea.SelectedValue != null)
            {
                this.selectedAreaId = (int)this.cmbArea.SelectedValue;
            }
        }

        /// <summary>
        /// 获得消息显示框
        /// </summary>
        /// <returns></returns>
        public UIRichTextBox GetMessageBox() => this.txtMessage;
    }
}
